<template>
  <div>
    <div class="handle-box">
      <el-button type="primary" class="handle-del mr10" @click="addClick">新增</el-button>
      <el-input v-model="query.kw" placeholder="请输入标签名称查询" class="handle-input mr10"></el-input>
      <el-button type="primary" icon="el-icon-search" @click="souClick">搜索</el-button>
    </div>
    <el-table v-loading="tableLoad" :data="tableData" border class="table" style="width: 100%" ref="multipleTable"
              header-cell-class-name="table-header" @selection-change="SelectionChange">
      <el-table-column type="selection" width="55" align="center"></el-table-column>
      <el-table-column type="index" :index="tableIndex" label="序号" width="55" align="center"></el-table-column>
      <el-table-column prop="ptName" label="标签名称"></el-table-column>
      <el-table-column prop="ptCreateTime" label="添加时间"></el-table-column>
      <el-table-column fixed="right" label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-edit" class="red" @click="editClick(scope.row)">编辑</el-button>
          <el-button type="text" icon="el-icon-delete" class="red" @click="delClick(scope.row)" :loading="scope.row.load">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination background layout="total, prev, pager, next" :current-page="query.page" :page-size="query.limit" :total="query.totalCount"
                     :pager-count="5" @current-change="PageChange" @prev-click="PageChange" @next-click="PageChange"></el-pagination>
    </div>

    <!-- 新增标签 -->
    <el-dialog title="新增标签" :visible.sync="addTagFlg" width="30%">
      <el-form ref="form" label-width="80px">
        <el-form-item label="标签名称">
          <el-input v-model="addTagText" maxlength="4" placeholder="请输入标签名称，最大长度为4位"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addTagFlg = false">取 消</el-button>
        <el-button type="primary" @click="formClick" :loading="addTagLoad">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { classManageGetProductTable, classManageGetProductAddSub, classManageGetProductEditSub, classManageGetProductDel } from '../../../../api/index';
export default {
  data() {
    return {
      tableLoad: false,
      query: {
        page: 1,
        limit: 10,
        totalCount: 0,
        kw: '',
      },
      tableData: [],
      addTagFlg: false,
      addTagText: '',
      addTagLoad: false,

      editId: '',
    };
  },
  created() {
    this.getInit();
  },
  methods: {
    //获取列表
    getInit() {
      this.tableLoad = true;
      let query = {
        page: this.query.page,
        limit: this.query.limit,
        kw: this.query.kw
      }
      classManageGetProductTable(query).then(res => {
        this.tableLoad = false;
        if (res.code == 0) {
          res.recordList.forEach(item => {
            item.load = false;
          })
          this.tableData = res.recordList;
          this.query.totalCount = res.totalCount;
        } else {
          this.$message(res.msg);
        }
      })
    },
    //序号
    tableIndex(index) {
      return (this.query.page - 1) * this.query.limit + index + 1;
    },
    //翻页
    PageChange(num) {
      this.query.page = num;
      this.getInit();
    },
    //搜索
    souClick() {
      this.query.page = 1;
      this.getInit();
    },
    //选中
    SelectionChange() { },
    //提交
    formClick() {
      if (this.editId) {
        this.editSubClick();
      } else {
        this.addSubClick();
      }
    },
    //新增
    addClick() {
      this.addTagFlg = true;
      this.editId = '';
    },
    //新增提交
    addSubClick() {
      this.addTagLoad = true;
      classManageGetProductAddSub({ name: this.addTagText }).then(res => {
        this.addTagLoad = false;
        if (res.code == 0) {
          this.$message.success("新增成功");
          this.addTagFlg = false;
          this.addTagText = '';
          this.getInit();
        } else {
          this.$message(res.msg);
        }
      })
    },
    //编辑
    editClick(item) {
      this.editId = item.ptId;
      this.addTagText = item.ptName;
      this.addTagFlg = true;
    },
    //编辑提交
    editSubClick() {
      this.addTagLoad = true;
      let query = {
        ptId: this.editId,
        ptName: this.addTagText
      }
      classManageGetProductEditSub(query).then(res => {
        this.addTagLoad = false;
        if (res.code == 0) {
          this.$message.success("编辑成功");
          this.addTagFlg = false;
          this.addTagText = '';
          this.getInit();
        } else {
          this.$message(res.msg);
        }
      })
    },
    //删除操作
    delClick(item) {
      item.load = true;
      classManageGetProductDel({ tagId: item.ptId }).then(res => {
        item.load = false;
        if (res.code == 0) {
          this.$message.success("删除成功");
          this.getInit();
        } else {
          this.$message(res.msg);
        }
      })
    }
  }
};
</script>

<style scoped>
.handle-input {
    width: 300px;
    display: inline-block;
}
</style>